package com.example.demo.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.demo.msg.ali.config.TemplateConstant;
import com.example.demo.msg.ali.entity.ValueWrapper;
import com.example.demo.msg.ali.service.IAliTemplateService;
import com.example.demo.order.entity.AFU;
import com.example.demo.order.entity.OrdersNew;
import com.example.demo.order.entity.PersonDto;
import com.example.demo.order.entity.RiskReport;
import com.example.demo.order.service.IOrdersNewService;
import com.example.demo.order.service.IRiskReportService;
import com.google.gson.Gson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 订单表-m- 前端控制器
 * </p>
 *
 * @author huamq
 * @since 2021-02-27
 */
@RestController
@RequestMapping("/order/orders-new")
@Slf4j
public class OrdersNewController {
    @Autowired
    private IOrdersNewService iOrdersNewService;

    @Autowired
    private IRiskReportService riskReportService;

    @Autowired
    private IAliTemplateService iAliTemplateService;

    @Autowired
    private Gson gson;

    private String getWrapperString(String value){
        return gson.toJson(new ValueWrapper(value));
    }

    @GetMapping("/test")
    public Object sendMsg() {
        Map<String, String> data = new HashMap<>();
        data.put("keyword1",getWrapperString("2088212512755181"));
        data.put("keyword2",getWrapperString("12342"));
        data.put("keyword3",getWrapperString("huawei Mate30 Pro 5G"));
        data.put("keyword4",getWrapperString("7999.00"));
        return iAliTemplateService.sendTemplate("2088802246540263", "MjA4ODgwMjI0NjU0MDI2M18xNjE1MDgxODMxMzk2XzA2OQ==",
                TemplateConstant.ORDER_TO_PAY,"page/mine/myorders/myorders", data);
    }

    @GetMapping("/hua")
    public Object getList() {
        LambdaQueryWrapper<OrdersNew> queryWrapper = new LambdaQueryWrapper<>();
        //queryWrapper.eq(OrdersNew::getOrderStatus, 9).or().eq(OrdersNew::getOrderStatus, 4);
        queryWrapper.eq(OrdersNew::getOrderStatus, 4);
        // queryWrapper.apply("date_format(order_create_time,'%Y-%m')=date_format(now(),'%Y-%m')");
        queryWrapper.apply("date_format(order_create_time, '%Y %m') = date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y %m')");
        List<OrdersNew> result = iOrdersNewService.list(queryWrapper);
        List<PersonDto> datas = new ArrayList<>();
        result.forEach(order -> {
            PersonDto data = new PersonDto();
            data.setAddress(order.getUserAddress());
            data.setName(order.getUserName());
            data.setPhone(order.getUserPhone());
            data.setId(order.getUserIdNumber());
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            data.setTime(df.format(order.getOrderCreateTime()));
            LambdaQueryWrapper<RiskReport> qw = new LambdaQueryWrapper<>();
            qw.eq(RiskReport::getOrderId, order.getOrderId());
            qw.eq(RiskReport::getType, 4);
            qw.eq(RiskReport::getIsInit, 0);
            RiskReport riskReport = riskReportService.getOne(qw);
            if (null != riskReport) {
                String s = riskReport.getRiskInfo();
                AFU t = new Gson().fromJson(s, AFU.class);
                data.setAfuScore(t.getCompositeScore());
                data.setAfuLevel(t.getDecisionSuggest());
            }

            LambdaQueryWrapper<RiskReport> qw1 = new LambdaQueryWrapper<>();
            qw1.eq(RiskReport::getOrderId, order.getOrderId());
            qw1.eq(RiskReport::getType, 8);
            qw1.eq(RiskReport::getIsInit, 0);
            RiskReport riskReportS = riskReportService.getOne(qw1);
            if (null != riskReportS) {
                data.setYidunScore(riskReportS.getRiskData());
            }
            if (data.getAfuLevel() == null) {
                data.setAfuLevel("null");
            }
            if (data.getAfuScore() == null) {
                data.setAfuScore("null");
            }
            if (data.getYidunScore() == null) {
                data.setYidunScore("null");
            }
            datas.add(data);
        });
        log.info(new Gson().toJson(datas));
        return new Gson().toJson(datas);
    }
}

